/**
 * 
 */
package app.tuwien.isi.tweetanalyser;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.util.Log;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

/**
 * @author Philipp Pospisil
 *
 */
public class LocationActivity extends MapActivity{

	private MapView mapView;
	private ArrayList<Tweet> tweets;
	
	private String[] place;
	private String[] sum;
	private String[] type;
	private String[] country;
	private String[] coordinates;
	
	private String[] user;
	private String[] location;
	private String[] language;
	private String[] followers;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.maps);
		
		mapView = (MapView) findViewById(R.id.mapview);      
		mapView.setBuiltInZoomControls(true);

		Intent intent = getIntent();

		if(intent.getParcelableArrayListExtra("tweets") != null){
			tweets = intent.getParcelableArrayListExtra("tweets");
			
			showTweetsOnMap();
		}else{
			if(intent.getStringExtra("statistic") != null){
				String statistic = intent.getStringExtra("statistic");
				if(statistic.equals("place")){
					place = intent.getStringArrayExtra("place");
					sum = intent.getStringArrayExtra("sum");
					type = intent.getStringArrayExtra("type");
					country = intent.getStringArrayExtra("country");
					coordinates = intent.getStringArrayExtra("coordinates");
					
					showPlacesOnMap();
				}else{
					if(statistic.equals("user")){
						user = intent.getStringArrayExtra("user");
						sum = intent.getStringArrayExtra("sum");
						location = intent.getStringArrayExtra("location");
						language = intent.getStringArrayExtra("language");
						followers = intent.getStringArrayExtra("followers");
						
						showUserOnMap();
					}
				}
			}else{
				Log.e("Intent Extras","are null at LocationActivity");
			}
		}
	}
	
	private void showTweetsOnMap (){
		List<Overlay> mapOverlays = mapView.getOverlays();
		Drawable drawable = this.getResources().getDrawable(R.drawable.ic_launcher);
		CustomItemizedOverlay itemizedOverlay = new CustomItemizedOverlay(drawable, this);
		
//		GeoPoint point = searchLocationByName(this, tweets.get(0).getPlace());
		for(int i=0; i<tweets.size();i++){
			if(tweets.get(i).getCoordinates() != null){
				GeoPoint point = searchLocationByName(this, tweets.get(i).getCoordinates());
				OverlayItem overlayitem = new OverlayItem(point, tweets.get(i).getUserName(), tweets.get(i).getText());
				
				itemizedOverlay.addOverlay(overlayitem);
				mapOverlays.add(itemizedOverlay);
						 
				MapController mapController = mapView.getController();
				
				mapController.animateTo(point);
				mapController.setZoom(6);
			}
		}
	}
	
	private void showPlacesOnMap (){
		List<Overlay> mapOverlays = mapView.getOverlays();
		Drawable drawable = this.getResources().getDrawable(R.drawable.ic_launcher);
		CustomItemizedOverlay itemizedOverlay = new CustomItemizedOverlay(drawable, this);
		
//		GeoPoint point = searchLocationByName(this, tweets.get(0).getPlace());
		for(int i=0; i<place.length;i++){
			if(coordinates[i] != null){
				GeoPoint point = searchLocationByName(this, coordinates[i]);
				OverlayItem overlayitem = new OverlayItem(point, place[i], sum[i] + 
						"\n" + type[i] + "\n" + country[i]);
				
				itemizedOverlay.addOverlay(overlayitem);
				mapOverlays.add(itemizedOverlay);
						 
				MapController mapController = mapView.getController();
				
				mapController.animateTo(point);
				mapController.setZoom(6);
			}
		}
	}
	
	private void showUserOnMap (){
		List<Overlay> mapOverlays = mapView.getOverlays();
		Drawable drawable = this.getResources().getDrawable(R.drawable.ic_launcher);
		CustomItemizedOverlay itemizedOverlay = new CustomItemizedOverlay(drawable, this);
		
//		GeoPoint point = searchLocationByName(this, tweets.get(0).getPlace());
		for(int i=0; i<user.length;i++){
			if(location[i] != null && !location[i].equals("")){
				GeoPoint point = searchLocationByName(this, location[i]);
				OverlayItem overlayitem = new OverlayItem(point, user[i], sum[i] + 
						"\n" + language[i] + "\n " + followers[i]);
				
				itemizedOverlay.addOverlay(overlayitem);
				mapOverlays.add(itemizedOverlay);
						 
				MapController mapController = mapView.getController();
				
				mapController.animateTo(point);
				mapController.setZoom(6);
			}
		}
	}
	
	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}

	//TODO Try this, you'd better run it in a separately thread rather than UI-thread.  http://stackoverflow.com/questions/10167819/android-geopoint-from-location-locality
	public static GeoPoint searchLocationByName(Context context, String locationName){
	    Geocoder geoCoder = new Geocoder(context, Locale.getDefault());
	    GeoPoint gp = null;
//	    Address ad = null;
	    try {
	        List<Address> addresses = geoCoder.getFromLocationName(locationName, 1);
	        for(Address address : addresses){
	            gp = new GeoPoint((int)(address.getLatitude() * 1E6), (int)(address.getLongitude() * 1E6));
//	            address.getAddressLine(1);
//	            ad = address;
	        }
	    } catch (IOException e) {
	        e.printStackTrace();
	    }
	    return gp;
	}
}
